package org.example.mallweb.mallservice.service.serviceimpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.example.mallcommon.constant.FavoriteConstant;
import org.example.mallcommon.projo.Favorite;
import org.example.mallcommon.result.ReturnResult;
import org.example.mallweb.mallservice.mybatiscommon.favorite.FavoriteMapper;
import org.example.mallweb.mallservice.service.FavorityService;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;


import javax.annotation.Resource;
import java.util.Date;

/**
 * @author 韩先楚
 */
@Service
public class FavoriteServiceImpl implements FavorityService {
    @Resource
    private FavoriteMapper favoriteMapper;


    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public Integer isFavority(Integer userId, Long articleId) {
        //先从redis中查询是否点赞过
        if (stringRedisTemplate.opsForHash().hasKey(FavoriteConstant.FAVORITE_TABLE_NAME + userId, articleId.toString())) {
            //如果redis查询到，就直接返回点赞状态
            Object o = stringRedisTemplate.opsForHash().get(FavoriteConstant.FAVORITE_TABLE_NAME + userId, articleId.toString());
            return Integer.parseInt(o.toString());
        }
        //如果redis没有查询到，就查询数据库
        Favorite favorite = favoriteMapper.selectOne(new QueryWrapper<Favorite>().eq("user_id", userId).eq("article_id", articleId));

        //如果数据库查不到,先插入到数据库再更新到redis
        if (favorite == null) {
            Favorite favorite1 = new Favorite();
            favorite1.setArticleId(articleId);
            favorite1.setUserId(userId);
            favorite1.setCreateTime(new Date());
            favoriteMapper.insert(favorite1);
            stringRedisTemplate.opsForHash().put(FavoriteConstant.FAVORITE_TABLE_NAME + userId, articleId.toString(), "0");
            return 0;
        }
        stringRedisTemplate.opsForHash().put(FavoriteConstant.FAVORITE_TABLE_NAME + userId, articleId.toString(), favorite.getStatus().toString());
        return Integer.parseInt(favorite.getStatus().toString());
    }

    /**
     * 点赞或取消点赞 (status: 1-点赞, 0-未点赞)
     */
    public ReturnResult<String> favorite(Integer status, Integer userId, Long articleId) {
        //直接修改redis中的状态
        if (stringRedisTemplate.opsForHash().hasKey(FavoriteConstant.FAVORITE_TABLE_NAME + userId, articleId.toString())) {
            stringRedisTemplate.opsForHash().put(FavoriteConstant.FAVORITE_TABLE_NAME + userId, articleId.toString(), status.toString());
            return ReturnResult.success("点赞成功");
        }
        //这里出现redis找不到是因为springtask执行了同步数据库操作,把同步完的数据删除才找不到
        //所以直接修改数据库然后同步到redis中
        //如果没有redis没有就直接修改数据库
        Favorite favorite = new Favorite();
        favorite.setStatus(status);
        favorite.setUserId(userId);
        favorite.setArticleId(articleId);
        int update = favoriteMapper.update(favorite, new QueryWrapper<Favorite>().eq("user_id", userId).eq("article_id", articleId));
        stringRedisTemplate.opsForHash().put(FavoriteConstant.FAVORITE_TABLE_NAME + userId, articleId.toString(), status.toString());
        return ReturnResult.success("收藏成功");
    }
}
